// packageB/components/task_tool/daiban/daiban.js
const mybehavior = require("../../../../behaviors/requests")
const general_data = require("../../../tools_package/general_data")
Component({
    /**
     * 组件的属性列表
     */
    behaviors: [mybehavior, general_data],
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        base_urlstr: "https://logisticsengineering.gree.com",
        list: [],
        filter_show: false,
        activeNames: ['1', '2', '3', '4'],
        btn_style: "default",
        task_id: "",
        filter_obj:{},
        filter_keyword: "",
        database: "",
        page_size: 5,
        page: 1,

    },
    lifetimes: {
        attached: function () {
            this.getDatas()
            // 在组件实例进入页面节点树时执行
            this.setData({
                filter_obj: this.getFIlterData()
            })


        },

    },
    /**
     * 组件的方法列表
     */
    methods: {
        updateData() {
            wx.showLoading({
                title: '加载中..'
            })

            const e = this.data.page + 1
            if (Math.ceil(this.data.total / this.data.page_size) >= e) {
                this.setData({
                    page: e
                })
                this.getDatas(e)
            } else {
                wx.showToast({
                    title: '已经到底啦~',
                    icon: 'none'
                })
            }


        },
        // 下滑触底获取已处理任务数据
        getDatas(e) {
            let url = this.data.base_urlstr + "/pfep2_api/personal_task_manage/query_involved_total_task";
            let data = {
                page: e,
                page_size: this.data.page_size,
                query_cond: {
                    table_name: { $regex: this.data.filter_keyword },
                    task_id: { $regex: this.data.task_id },
                },
                database: this.data.database
            }
            this.post(url, data).then(res => {
                let arr = res.data.results
                for (let i in arr) {
                    let steps = arr[i].task.recorders
                    for (let j in steps) {

                        steps[j].time = this.GMTToStr(steps[j].time)
                        if (steps[j].reason == "flow") {
                            steps[j].reason = "进入流程"
                        }

                    }
                    arr[i].flag = "done"
                    arr[i].start_time = this.GMTToStr(arr[i].start_time)
                    arr[i].lastUpdateTime = this.GMTToStr(arr[i].lastUpdateTime)
                    if (arr[i].business == "工装容器监督检查") {
                        if (arr[i].status == "no" || "yes") {
                            arr[i].status = "done"
                        } else {
                            arr[i].status = "doing"

                        }
                        if (arr[i].task.deadline != "") {
                            arr[i].business_content = '处理期限：' + arr[i].task.deadline + "，" + arr[i].business_content

                        }
                    }
                    if (arr[i].business == "5S监督检查") {
                        arr[i].task.current_involved_name = arr[i].current_involved_name
                        const status_arr = [
                            { "问题提交": "问题提交，待责任单位整改" },
                            { "责任单位整改报完工": "责任单位整改报完工，待确认第1次整改完成情况" },
                            { "确认整改完成": "完结，确认完成整改" },
                            { "确认未完成整改": "第1次整改未完成，待责任单位第2次整改" },
                            { "责任单位第2次整改报完工": "责任单位整改报完工，待确认第2次整改完成情况" },
                            { "确认第二次整改完成": "完结，确认完成整改" },
                            { "确认第二次整改未完成": "完结，确认未完成整改" },
                            { "申请延期": "责任单位申请延期，待确认" },
                            { "同意延期": "同意延期，待责任单位整改" },
                            { "不同意延期": "不同意延期，待责任单位整改" },
                            { "责任单位整改报完工-已延期": "责任单位整改报完工，待确认整改完成情况-已延期" },
                            { "确认整改完成-已延期": "完结，确认完成整改" },
                            { "确认未完成整改-已延期": "完结，确认未完成整改" },
                        ]
                        for (let j = 0; j < status_arr.length; j++) {
                            let keys_arr = Object.keys(status_arr[j])
                            if (keys_arr[0] == arr[i].task.status) {
                                arr[i].task.status1 = status_arr[j][keys_arr[0]]
                            }

                        }
                        if (arr[i].lastProcessAction == "提交人撤回，关闭问题") {
                            arr[i].lastProcessAction = "提交人撤回，关闭问题"
                        } else {
                            arr[i].lastProcessAction = arr[i].task.status1

                        }
                        let now_time = this.formatNowTime()
                        // arr[i].task.rect_deadline = this.GMTToStr(arr[i].task.rect_deadline)
                        const d = this.getDaysBetweenDates(now_time, arr[i].task.rect_deadline)
                        if (d >= 0.0) {
                            arr[i].business_content = '处理期限：' + arr[i].task.rect_deadline + '，超期天数：' + 'null，' + "流程：" + arr[i].task.status1 + '，责任单位：' + arr[i].task.res_accurate + '，问题类别：' + arr[i].task.problem_category
                        } else {
                            arr[i].business_content = '处理期限：' + arr[i].task.rect_deadline + '，超期天数：' + Math.abs(d) + '天，' + "流程：" + arr[i].task.status1 + '，责任单位：' + arr[i].task.res_accurate + '，问题类别：' + arr[i].task.problem_category
                        }

                    }
                    if (arr[i].business_content.length > 90) {
                        arr[i].business_content1 = arr[i].business_content.slice(0, 90) + "..."
                    } else {
                        arr[i].business_content1 = arr[i].business_content

                    }
                    if (arr[i].database == "pfep2") {
                        if (arr[i].current_involved_name.length != 0) {
                            arr[i].lastProcessAction = "【" + arr[i].current_process
                                + "】- " + arr[i].current_involved_name[0]
                                + "- 未签"
                        } else {
                            arr[i].lastProcessAction = "已完结"

                        }

                    } else {
                        if (arr[i].lastProcessAction == "agree") {
                            arr[i].lastProcessAction = "同意"
                        }
                        if (arr[i].lastProcessAction == "disagree") {
                            arr[i].lastProcessAction = "驳回"
                        }
                    }
                }
                let arr_list = []
                arr_list = this.data.list.concat(arr)
                if (res.code == 20000) {

                    this.setData({
                        list: arr_list,
                        total: res.data.total
                    })
                    wx.hideLoading()

                } else {
                    wx.hideLoading()

                }
            }).catch(err => {
                wx.showToast({
                    title: err.msg,
                    icon: 'none'
                })
            })



        },


        // 点击筛选条件前重置筛选条件
        filterReset() {
            this.setData({
                page: 1,
                page_size: 5,
                total: 0,
                filter_show: false,
                filter_obj:this.getFIlterData(),

                list: [],
                filter_keyword: "",
                database: "",
            })
        },
        // 筛选重置按钮
        filterResetBtn() {
            this.filterReset() //初始化数据
            this.setData({
                task_id: ""
            })
            this.getDatas()     //重新获取数据
        },
        // 点击筛选内容搜索数据
        filterKey(e) {
            console.log(e);
            this.filterReset() //初始化筛选数据
            this.setData({
                task_id: ""
            })
            let index = e.currentTarget.dataset.index
            let key = e.currentTarget.id
            let database = e.currentTarget.dataset.row.database
            let table_name = e.currentTarget.dataset.row.table_name

            let original_filter = this.data.filter_obj
            original_filter[key][index].type = 'info'
            this.setData({
                filter_obj: original_filter,
                filter_show: false,
                filter_keyword: table_name,
                database: database,

            })
            this.getDatas()
        },

        // 筛选弹窗弹出
        filterShow() {
            console.log("筛选");
            this.setData({
                filter_show: true,
            })
        },
        // 筛选弹窗关闭
        onFilertClose() {
            this.setData({
                filter_show: false,
            })
        },
        // 筛选弹窗内容展开
        onChange(event) {
            this.setData({
                activeNames: event.detail,
            });
        },

        // 手动输入受理编号搜索
        filterInputSearch() {
            this.filterReset()  //初始化筛选数据
            this.getDatas()

        },

        // 点击任务详情跳转
        detailShow(e) {
            wx.requestSubscribeMessage({
                tmplIds: this.getTmplIds(),

            })
            console.log(e.currentTarget.dataset.row);
            let data = e.currentTarget.dataset.row

            const routeMap = this.getRouteMap()
            const url = routeMap[data.business];

            if (!url) {
                this.toPersonalApp()

                return;
            }
            wx.navigateTo({
                url: url,
                success(res) {
                    res.eventChannel.emit('showDetail', data);
                },
                fail(err) {
                    wx.showToast({
                        title: '页面跳转失败',
                        icon: 'none'
                    })
                }

            })

        },








    }
})
